var $hole = $('#hole');
var $loginFrame = $('#loginFrame');
var $loginBtn = $('#loginBtn');
var $nav = $('#nav');
var user;

var setLisData = function(data){
	for(x in data){
		$nav.append('<li><a target="main" href="list.html?action='+data[x]+'">'+x+'</a></li>');
	}
}

var initLis = function(data){

	setLisData(data);
	
	$nav.find('li').on('click',function(){
		$nav.find('li').removeClass('active');
		$(this).addClass('active');
	});
}

var initWqName = function(catId,user){
	if(user == 'superadmin'){
		$('#wq_name').html('超级管理员');
		return ;
	}
	$.get(BASE_URL + "/" + catId + "Select",{},function(res){
		var data = res.data;
		for(i in data){
			if(data[i]['id'] == user){
				$('#wq_name').html(data[i]['name']);
			}
		}
	},'json');
}

var showHole = function(){
	$hole.show();
}
var hideHole = function(){
	$hole.hide();
}
$loginBtn.on('click',function(){
	var username = $loginFrame.find('input[name=username]').val();
	var password = $loginFrame.find('input[name=password]').val();
	var radio = $loginFrame.find('input[name="role"]:checked').val();
	$.post(ROLE_CONTROLLER[radio].LOGIN_URL,{'user.username':username,'user.password':password},function(res){
		var data = isSuc(res);
		console.log(data);
		alert(res.msg);
		if(data){
			user = data.user;
			initLis(data.cats);
			initWqName(data.catId,data.user);
			hideHole();
		}
	},'json');
});

$.get(ROLE_CONTROLLER.CHECKLOGIN,{},function(res){
	var data = isSuc(res);
	if(data){
		initLis(data.cats);
		initWqName(data.catId,data.user);
		hideHole();
	}
},'json');